﻿--[[
   @brief    shadow helper 
   @author   hqzheng
   @version  1.0
   @date     2019-07-04
--]]

shadow_helper = {
    wnd_id_2_shadow_id = {};

    create_shadow = function (wnd_id)
        if shadow_helper.is_shadow_exist(wnd_id) then
            return;
        end
        ui_utility.setproperty_by_id(wnd_id,
            "OnPosChanged", "shadow_helper.on_wnd_move");
        local shadow_id = layout_mgr.restore_layout_multi(
            "wegame_social\\Shadow.tpf");
        shadow_helper.wnd_id_2_shadow_id[wnd_id] = shadow_id;
        local shadow_ptr = ui_utility.get_ctrl_ptr_by_id(shadow_id);
        if nil ~= shadow_ptr then
            shadow_helper.on_wnd_move(nil, wnd_id);
            shadow_ptr:Show(TRUE);
        end
    end;

    destroy_shadow = function (wnd_id)
        local shadow_id = 0;
        shadow_id = shadow_helper.wnd_id_2_shadow_id[wnd_id];
        if nil == shadow_id or 0 == shadow_id then
            return;
        end
        layout_mgr.delay_remove_ctrl(shadow_id);
        shadow_helper.wnd_id_2_shadow_id[wnd_id] = nil;
    end;

    on_wnd_move = function (uilib, control_id)
        local shadow_id = 0;
        shadow_id = shadow_helper.wnd_id_2_shadow_id[control_id];
        if nil == shadow_id or 0 == shadow_id then
            return;
        end
        local shadow_ptr = ui_utility.get_ctrl_ptr_by_id(shadow_id);
        local wnd_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);

        if nil ~= shadow_ptr and nil ~= wnd_ptr then
            local     rt_this = wnd_ptr:GetRectEx(TRUE,TRUE);
            local     rt_temp = shadow_ptr:GetRectEx(TRUE,TRUE);
            local     width = rt_this.right - rt_this.left + 32;
            local     height= rt_this.bottom - rt_this.top + 32;

            rt_temp.left  = rt_this.left - 16;
            rt_temp.top   = rt_this.top  - 6;
            rt_temp.right = rt_temp.left + width;
            rt_temp.bottom= rt_temp.top  + height;

            shadow_ptr:MoveEx(rt_temp, TRUE);
        end;
    end;

    is_shadow_exist = function (wnd_id)
        local is_exist = false;
        for k, v in pairs(shadow_helper.wnd_id_2_shadow_id) do
            if k == wnd_id then
                is_exist = true;
                break;
            end;
        end
        return is_exist;
    end;

    show_shadow_internal = function(wnd_id, show)
        local shadow_id = 0;
        shadow_id = shadow_helper.wnd_id_2_shadow_id[wnd_id];
        if nil == shadow_id or 0 == shadow_id then
            return;
        end
        ui_utility.show_ctrl(shadow_id, show);
    end;

    activate_window = function(wnd_id)
        if nil == wnd_id or 0 == wnd_id then
            return;
        end
        local wnd_ptr = ui_utility.get_ctrl_ptr_by_id(wnd_id);
        if nil ~= wnd_ptr then
            local dlg_ptr = tolua.cast(wnd_ptr, "Tenio::ITDialog");
            if nil ~= dlg_ptr then
                local tpf_ui_lib = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
                if nil == tpf_ui_lib then
                    return;
                end
                local HWND_NOTOPMOST = tpf_ui_lib:number_to_hwnd(-2);
                local HWND_TOPMOST = tpf_ui_lib:number_to_hwnd(-1);
                local SWP_NOMOVE = 0x0002;
                local SWP_NOSIZE = 0x0001;
                local SWP_NOACTIVATE = 0x0010;
                local flags = bit_operation_utility.or_op(SWP_NOMOVE, SWP_NOSIZE);
                flags = bit_operation_utility.or_op(flags, SWP_NOACTIVATE);
                dlg_ptr:SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, flags);
                dlg_ptr:SetWindowPos(HWND_NOTOPMOST, 0, 0, 0, 0, flags);
            end
        end
    end;

    activate_shadow = function(wnd_id)
        local shadow_id = 0;
        shadow_id = shadow_helper.wnd_id_2_shadow_id[wnd_id];
        if nil == shadow_id or 0 == shadow_id then
            return;
        end
        shadow_helper.activate_window(shadow_id);
    end;

    show_shadow = function(wnd_id)
        shadow_helper.show_shadow_internal(wnd_id, true);
        shadow_helper.activate_shadow(wnd_id);
    end;

    hide_shadow = function(wnd_id)
        shadow_helper.show_shadow_internal(wnd_id, false);
    end;
};